{% extends "base.html" %}
{% block content %}
        {% if saved %}
        <p id="saved"> {{ saved }} Study reviews saved, thanks! </p>
        {% endif %}
        <h3>Welcome {{ user.first_name }}, please review the following studies:</h3>

        <div class=alert id="response" style="display:none;"></div>
        {% if studies|length %}
        <form method="post" onsubmit="return sendReviews();">{% csrf_token %}
            <table class="table table-striped table-hover">
                <thead>
                    <tr><th>ID</th>
                    {% if high_risk or always_show_high_risk %}
                        <th>Flagged as High Risk?</th>
                    {% endif %}
                        <th>Study Link</th>
                        <th>Reviewed?</th>
                        <th>Burnt-In PHI?</th>
                        <th>Facial Reconstruction?</th>
                        <th>Protocol Series?</th>
                        <th>Relevant to {{ project_name }}?</th>
                        <th>Exclude for other reason? Please comment.</th>
                        <th>Comment</th>
                    </tr>
                </thead>
            <tbody>
                {% for study in studies %}
                <tr>
                    <td><div class="database_id">{{study.id}}</div></td>
                    {% if high_risk or always_show_high_risk %}
                    <td>
                        {% if study.high_risk_flag %}
                            <div class="alert alert-error">
                                {% if study.high_risk_message %}
                                <i class=icon-fire></i>{{ study.high_risk_message }}
                                {% else %}
                                <i class=icon-fire></i>Flagged as high risk for PHI
                                {% endif %}
                            </div>
                        {% else %}
                            <div class="alert alert-success">
                                <i class=icon-thumbs-up></i>Not flaggged
                            </div>
                        {% endif %}
                    </td>
                    {% endif %}
                    <td><a href="{{ studycentric_link }}?studyUID={{study.original_study_uid}}&instances=true" target="_blank">View study {{ study.accession_no }} from {{study.study_date.year}}</a></td>
                    <td><input class="reviewed" style="margin:20px 0px 20px 0px;" name="reviewed" type="checkbox"/></td>
                    <td><select name="phi">
                           <option value="unknown" selected="selected">Unknown</option>
                           <option value="yes">Yes</option>
                           <option value="no">No</option>
                    </select></td>
                    <td><input name="reconstruction" type="checkbox"/></td>
                    <td><input name="protocol" type="checkbox"/></td>
                    <td><input name="relevant" type="checkbox"/></td>
                    <td><input name="exclude" type="checkbox"/></td>
                    <td><textarea name="comment" rows="2" cols="30"></textarea></td>
                </tr>
                {% endfor %}
                <tr><td/><td/>{%if high_risk or always_show_high_risk %}<td/>{% endif %}<td/><td/><td/><td/><td/><td></td><td><input style="float:right" class="btn btn-success" type="submit" value="Save reviewed studies"/></td></tr>
            </tbody>
        </table>
    </form>
    {% else %}

    <div class="alert alert-success">No studies to review. Thanks!</div>
    {% endif %}
    <div class="alert alert-warn" id="warn" style="display:none;"></div>
    <script type="text/javascript" charset="utf-8">
         $(document).ajaxSend(function(event, xhr, settings) {
                 xhr.setRequestHeader("X-CSRFToken", $('[name="csrfmiddlewaretoken"]').val());
         });

         {% if warn_unviewed %}
         $('input.reviewed').popover({
             content:  "<i class=icon-bell></i>This study has not yet been viewed.",
             html: true,
             placement: "bottom",
             trigger: "manual"
         });
         $('table').on('change', 'input.reviewed', function(){
             var that = this;
              if (!$(this).is(":checked"))
                  return;
              if ($(this).parent().parent().find('a').is(".clicked"))
                  return;
              $(this).popover("show");
              setTimeout(function(){$(that).popover('hide');}, 3000);
         });
         {% endif %}

         // takes care of licks and enter
         $('a').click(function(){
                 $('tr').removeClass("success");
                 $(this).parent().parent().addClass("success");
                 $(this).addClass("clicked");
         });

         // Don't bother the user if they have right clicked on the link
         // We will assume the have either copied the link or opened in a new
         // window/tab
         $('a').mousedown(function(event){
             if (event.which === 3){
                 $('tr').removeClass("success");
                 $(this).parent().parent().addClass("success");
                 $(this).addClass("clicked");
             }
         });

         var null_bool_map = {
             yes: true,
             no: false,
             unknown:null
         };

         var payload;
         var sendReviews = function(){
             payload = [];
             $("table tr").each(function(){
                     if (!$('[name="reviewed"]', this).is(":checked")) return;
                     payload.push({
                          study:parseInt($(".database_id", this).text()),
                          has_reconstruction:$('[name="reconstruction"]',this).is(":checked"),
                          has_protocol_series:$('[name="protocol"]',this).is(":checked"),
                          relevant:$('[name="relevant"]',this).is(":checked"),
                          has_phi:null_bool_map[$('[name="phi"]',this).val()],
                          exclude:$('[name="exclude"]',this).is(":checked"),
                          comment:$('[name="comment"]',this).val()
                     });
             });
             $.ajax({
                 url:window.location.href,
                 type:"POST",
                 data:JSON.stringify(payload),
                 contentType:"application/json",
                 success: function(data){
                 $("#response").addClass("alert-info").text(data.saved + " reviews saved. Thanks!").fadeIn("fast");
                    setTimeout(function(){ window.location.href = window.location.href;}, 2000);
                 },
                 error: function(){
                     $("#response").addClass("alert-error").text("An error occurred. Please contact an administrator.").fadeIn("fast");
                 }
             });
             $("table").fadeOut("fast");
             return false;
         };
     </script>
{% endblock %}

